package com.tj.test.sys.service.impl;

import com.tj.test.sys.dao.RolerpowerMapper;
import com.tj.test.sys.po.Rolerpower;
import com.tj.test.sys.service.IRolerpowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class RolerpowerService implements IRolerpowerService {
    @Autowired
    private RolerpowerMapper mapper;

    @Override
    //查询所有角色权限信息
    public List<Rolerpower> selectAllRolerpower() {
        List<Rolerpower> list = new ArrayList<>();
        if (!mapper.selectAllRolerpower().isEmpty()) {
            list = mapper.selectAllRolerpower();
        }
        return list;
    }


    @Override
    //插入角色权限信息
    public int insertRolerpower(Rolerpower rolerpower) {
        int temp = 0;
        /* 业务判断
         * （1）权限名称不能重复
         * （2）增加权限之前先查询*/
        try{
        temp = mapper.selectRolerpowerById(rolerpower);
        if (temp < 1) {
            temp=mapper.insertRolerpower(rolerpower);}
        } catch (Exception e) {
            temp = -1;
            System.out.println(e.getStackTrace());
        }
        return temp;
    }

    @Override
    //删除角色权限信息
    public int deleteRolerpower(Rolerpower rolerpower) {
        int temp = 0;
        /*1.有关业务流程的判断*/
        //(1)删除有关业务
        try {
            temp = mapper.selectRolerpowerById(rolerpower);
            if (temp > 0) {
                mapper.deleteRolerpower(rolerpower);
            }
        } catch (Exception e) {
            temp = -1;
            System.out.println(e.getStackTrace());
        }

        return temp;
    }

    @Override
    //修改角色权限信息
    public int updateRolerpower(Rolerpower rolerpower) {
        int temp = 0;
        try {
            temp = mapper.selectRolerpowerByIdCount(rolerpower.getPrpid());
            if (temp > 0) {
                temp=mapper.updateRolerpower(rolerpower);
            }
        } catch (Exception e) {
            temp = -1;
            //打印错误信息
            System.out.println(e.getStackTrace());
        }

        return temp;
    }
}
